package pl.streamsoft.currencyApp.model;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

@Entity
@Table(name = "CURRENCIES")
@NamedQueries({
		@NamedQuery(name = "Currency.getCurrenciesByDateOfPublication", query = "from Currency where currencyValue.dateOfPublication = :dateOfPublication"),
		@NamedQuery(name = "Currency.getExistingDatesFromTo", query = "select currencyValue.dateOfPublication from Currency where currencyValue.dateOfPublication >= :fromDate and currencyValue.dateOfPublication <= :toDate"),
		@NamedQuery(name = "Currency.getCurrenciesByDateFromTo", query = "from Currency where currencyValue.dateOfPublication >= :fromDate and currencyValue.dateOfPublication <= :toDate"),
		@NamedQuery(name = "Currency.getCurrenciesByCode", query = "from Currency where currencyValue.dateOfPublication = :dateOfPublication and currencyInfo.code = :codeName"),
		@NamedQuery(name = "Currency.getCurrenciesByName", query = "from Currency where currencyValue.dateOfPublication = :dateOfPublication and currencyInfo.name = :currencyName"),
		@NamedQuery(name = "Currency.getCurrenciesByKeyWord", query = "from Currency where currencyValue.dateOfPublication = :dateOfPublication and (currencyInfo.name like concat('%', :keyWord, '%') or currencyInfo.code like concat('%', :keyWord, '%'))"),
		@NamedQuery(name = "Currency.getCurrenciesByMinValue", query = "from Currency where currencyValue.dateOfPublication = :dateOfPublication and currencyValue.averageRate >= :minValue"),
		@NamedQuery(name = "Currency.getCurrenciesByMinAndMaxValue", query = "from Currency where currencyValue.dateOfPublication = :dateOfPublication and currencyValue.averageRate >= :minValue and currencyValue.averageRate <= :maxValue") })

public class Currency {

	@Id
	@Column(name = "CURRENCY_ID")
	@GeneratedValue
	private long currencyId;

	@Embedded
	@NotFound(action = NotFoundAction.EXCEPTION)
	private CurrencyValue currencyValue;
	
	@Cascade(CascadeType.SAVE_UPDATE)
	@Fetch(FetchMode.JOIN)
	@NotFound(action = NotFoundAction.EXCEPTION)
	@OneToOne
	@JoinColumn(name="CURRENCY_INFO_ID")
	private CurrencyInfo currencyInfo;

	public Currency() {}

	public Currency(CurrencyInfo info, CurrencyValue val){
		this.currencyInfo = info;
		this.currencyValue = val;
	}

	public CurrencyValue getCurrencyValue() {
		return currencyValue;
	}

	public CurrencyInfo getCurrencyInfo() {
		return currencyInfo;
	}
}
